<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="任务编号" prop="taskCode">
        <el-input
          v-model="queryParams.taskCode"
          placeholder="请输入任务编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务类型" prop="taskType">
        <el-select v-model="queryParams.taskType" placeholder="请选择任务类型" clearable size="small">
          <el-option
            v-for="dict in taskTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="任务状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择任务状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="taskList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="任务编号" align="center" prop="taskCode" />
      <el-table-column label="任务类型" align="center" prop="taskType">
        <template slot-scope="scope">
          {{ taskTypeFormat(scope.row) }}
        </template>
      </el-table-column>
      <el-table-column label="关联单号" align="center" prop="relatedCode" />
      <el-table-column label="检验物料/产品" align="center" prop="itemName" />
      <el-table-column label="计划检验时间" align="center" prop="planTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.planTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="任务状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status === '0' ? 'warning' : scope.row.status === '1' ? 'primary' : 'success'">
            {{ statusFormat(scope.row) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['sb:pendingTasks:query']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleExecute(scope.row)"
            v-hasPermi="['sb:pendingTasks:execute']"
          >执行</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
export default {
  name: "PendingTasks",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 任务列表
      taskList: [],
      // 任务类型选项
      taskTypeOptions: [
        { dictLabel: '来料检验', dictValue: '0' },
        { dictLabel: '过程检验', dictValue: '1' },
        { dictLabel: '出货检验', dictValue: '2' },
        { dictLabel: '退料检验', dictValue: '3' }
      ],
      // 状态选项
      statusOptions: [
        { dictLabel: '待执行', dictValue: '0' },
        { dictLabel: '执行中', dictValue: '1' },
        { dictLabel: '已完成', dictValue: '2' }
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskCode: undefined,
        taskType: undefined,
        status: undefined
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询任务列表 */
    getList() {
      this.loading = true;
      // 模拟数据，实际应从后端获取
      setTimeout(() => {
        this.taskList = [
          {
            id: 1,
            taskCode: 'RW20230701001',
            taskType: '0',
            relatedCode: 'LRJY20230701001',
            itemName: '钢板',
            planTime: new Date('2023-07-01'),
            status: '0'
          },
          {
            id: 2,
            taskCode: 'RW20230702001',
            taskType: '1',
            relatedCode: 'GCJY20230702001',
            itemName: '半成品A',
            planTime: new Date('2023-07-02'),
            status: '1'
          },
          {
            id: 3,
            taskCode: 'RW20230703001',
            taskType: '2',
            relatedCode: 'CHJY20230703001',
            itemName: '电动机',
            planTime: new Date('2023-07-03'),
            status: '2'
          }
        ];
        this.total = this.taskList.length;
        this.loading = false;
      }, 300);
    },
    // 任务类型字典翻译
    taskTypeFormat(row) {
      return this.selectDictLabel(this.taskTypeOptions, row.taskType);
    },
    // 状态字典翻译
    statusFormat(row) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 字典翻译
    selectDictLabel(datas, value) {
      if (value === undefined) {
        return "";
      }
      var actions = [];
      Object.keys(datas).some(key => {
        if (datas[key].dictValue == ('' + value)) {
          actions.push(datas[key].dictLabel);
          return true;
        }
      })
      return actions.join('');
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 查看按钮操作 */
    handleView(row) {
      const taskType = row.taskType;
      const relatedCode = row.relatedCode;
      // 根据任务类型跳转到不同的检验单详情页
      if (taskType === '0') {
        this.$router.push({ path: `/sb/IncomingInspection/detail/${relatedCode}` });
      } else if (taskType === '1') {
        this.$router.push({ path: `/sb/ProcesInspection/detail/${relatedCode}` });
      } else if (taskType === '2') {
        this.$router.push({ path: `/sb/OutgoingInspection/detail/${relatedCode}` });
      } else if (taskType === '3') {
        this.$router.push({ path: `/sb/ReturnInspection/detail/${relatedCode}` });
      }
    },
    /** 执行按钮操作 */
    handleExecute(row) {
      const taskType = row.taskType;
      // 根据任务类型跳转到不同的检验单编辑页
      if (taskType === '0') {
        this.$router.push({ path: `/sb/IncomingInspection/edit/${row.relatedCode}` });
      } else if (taskType === '1') {
        this.$router.push({ path: `/sb/ProcesInspection/edit/${row.relatedCode}` });
      } else if (taskType === '2') {
        this.$router.push({ path: `/sb/OutgoingInspection/edit/${row.relatedCode}` });
      } else if (taskType === '3') {
        this.$router.push({ path: `/sb/ReturnInspection/edit/${row.relatedCode}` });
      }
    }
  }
};
</script> 